# -*- coding: utf-8 -*-
# <nbformat>3.0</nbformat>

# <codecell>

import Image
import ImageFilter
import ImageMath

# <codecell>

orig_img = Image.open("girl.png")

# <codecell>

imshow(orig_img)

# <codecell>

blur_filter = orig_img.filter(ImageFilter.BLUR)

# <codecell>

imshow(blur_filter)
blur_filter.save("blur_filter.png")

# <codecell>

median_filter_3 = orig_img.filter(ImageFilter.MedianFilter(size=3))

# <codecell>

imshow(median_filter_3)
median_filter_3.save("median_filter_3.png")

# <codecell>

median_filter_5 = orig_img.filter(ImageFilter.MedianFilter(size=5))

# <codecell>

imshow(median_filter_5)
median_filter_5.save("median_filter_5.png")

# <codecell>

def sub_image_RGB(img1, img2):
    R1, B1, G1 = img1.split()
    R2, B2, G2 = img2.split()
    
    diff_R = ImageMath.eval("convert(abs(a - b), 'L')", a=R1, b=R2)
    diff_G = ImageMath.eval("convert(abs(a - b), 'L')", a=G1, b=G2)
    diff_B = ImageMath.eval("convert(abs(a - b), 'L')", a=B1, b=B2)

    sub_img = Image.merge("RGB", (diff_R, diff_G, diff_B))
    
    return sub_img

# <codecell>

diff_blur_filter = sub_image_RGB(orig_img, blur_filter)

# <codecell>

imshow(diff_blur_filter)
diff_blur_filter.save("diff_blur_filter.png")

# <codecell>

diff_median_filter_3 = sub_image_RGB(orig_img, median_filter_3)
imshow(diff_median_filter_3)
diff_median_filter_3.save("diff_median_filter_3.png")

# <codecell>

diff_median_filter_5 = sub_image_RGB(orig_img, median_filter_5)
imshow(diff_median_filter_5)
diff_median_filter_5.save("diff_median_filter_5.png")

